package com.cheng.lesson04;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;

public class URLDemon02 {
    public static void main(String[] args) throws IOException {
        //下载地址
        URL url = new URL("http://localhost:9090/pengcheng/ConfidentialFile.txt");
        //通过url打开连接，连接资源
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream("ConfidentialFile.txt");

        byte[] buffer = new byte[1024];
        int len;
        while((len=inputStream.read(buffer))!=-1){
            fileOutputStream.write(buffer,0,len);

        }
        //断开连接
        fileOutputStream.close();
        inputStream.close();
        urlConnection.disconnect();
    }

}
